/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;


/**
 *
 * @author Alexandre CZECH
 */
public class ContactList {
    // Class parameters
    // This array contains the contact list from the csv file
    ArrayList<Contact> ContactList=new ArrayList<>(); // this is an array of object Contact
    
    
    /*
     * Constructor Build the Contact List thanks to the file
     */
    public ContactList()throws FileNotFoundException, IOException{
        
        
        BufferedReader reader = new BufferedReader(new FileReader("contactList.csv"));
        String ch = null;

        while((ch = reader.readLine()) != null){
            
            // put the whole file in a tab each entity is 
            String[] simulationInfoData = ch.split(";");
            // Pick up the info of the line and create the contact object          
            Contact theContact = new Contact(simulationInfoData[0],
                simulationInfoData[1],
                        simulationInfoData[2],
                        simulationInfoData[3],
                                simulationInfoData[4],
                                simulationInfoData[5],
                                        simulationInfoData[6],
                                        simulationInfoData[7]);
            // Add this contact to the array
            ContactList.add(theContact);
    }// We add all the Contacts to the array reading the file
        
    }
    
    
    
    
    
    
  
    
  
    
    /*
     * Get the name of a given contact
     */
    public String getMyname(int i){    
        return ContactList.get(i).getNAME();
        }
    
    /*
     * Get the phone number of a given contact
     */
    public String getMyPhone(int i){    
        return ContactList.get(i).getPHONE_PERSO();
        }
    
    
    /*
     * This method find the corresponding contact thanks to its phone number pro used as an ID
     * @ retunr the object Contact
     */
    public Contact ReturnContact(String phoneNumberPerso){
        
        int i;
        Contact myContact=null;
        for (i=0;i<ContactList.size();i++){
            if(ContactList.get(i).getPHONE_PERSO()==phoneNumberPerso)
                myContact= ContactList.get(i) ;
        }
        return myContact;
        
    }
    
    /*
     * This methods permits to return the contact list 
     */

    public ArrayList<Contact> ReturnMyList(){
        return ContactList;
    }
    
    /*
     * Construct the tab with the names and surnames of The contact
     * @return String[]
     */
    public ArrayList<String> ConstructList(){
        int i;
        String myString;
        ArrayList<String> List= new ArrayList<>();
        
        for (i=0;i<ContactList.size();i++){
            String name=ContactList.get(i).getNAME();// pick up the name         
            String surName=ContactList.get(i).getLAST_NAME();// pick up the Surname
            myString=name+" "+surName;
            List.add(myString);
        }  
    return List;
}
    
    
    
    
    /*
     * Add a contact to the arrayList ContactList
     */
    public void AddContact(Contact contact)throws IOException{
       
        char space = ';';
        try (BufferedWriter writer = new BufferedWriter(new FileWriter("contactList.csv", true))) {
                writer.write(String.valueOf(contact.getNAME())+ space);
                writer.write(String.valueOf(contact.getLAST_NAME())+ space);
                writer.write(String.valueOf(contact.getHOME_ADD())+ space);
                writer.write(String.valueOf(contact.getCOMPANY_ADD())+ space);
                writer.write(String.valueOf(contact.getPHONE_PERSO())+ space);
                writer.write(String.valueOf(contact.getPHONE_PRO())+ space);
                writer.write(String.valueOf(contact.getHOME_PAGE())+ space);
                writer.write(String.valueOf(contact.getEMAIL())+ space);
                writer.newLine();
        }
        
    }
    
    
    /*
     * Delete a contact from the list
     */
    public void DeleteContact(Contact contact){
        ContactList.remove(contact);
    }
    
    /*
     * Getter: give the whole current contact list
     */
 
    public ArrayList<Contact> ReturnList () throws FileNotFoundException, IOException {
        BufferedReader reader = new BufferedReader(new FileReader("contactList.csv"));
        String ch = null;
        ArrayList<Contact> myList = new ArrayList<>();

        while((ch = reader.readLine()) != null){
            
            // put the whole file in a tab each entity is 
            String[] simulationInfoData = ch.split(";");
            // Pick up the info of the line and create the contact object          
            Contact theContact = new Contact(simulationInfoData[0],
                simulationInfoData[1],
                        simulationInfoData[2],
                        simulationInfoData[3],
                                simulationInfoData[4],
                                simulationInfoData[5],
                                        simulationInfoData[6],
                                        simulationInfoData[7]);
            // Add this contact to the array
           
    }// We add all the Contacts to the array reading the file
      return myList;  
    }
    }

